﻿using System;
using System.Data;
using System.Configuration;
using System.Text;
using System.IO;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using SSN.DotNet;

namespace SSN.WebFramework.Controls
{
    public class YesNo : System.Web.UI.WebControls.RadioButtonList, IBindable
    {
        /// <summary>
        /// Gets or sets the selected answer as a YesNoEnum value.
        /// </summary>
        public YesNoEnum SelectedAnswer
        {
            get
            {
                return this.SelectedValue.ToYesNoEnum();
            }
            set
            {
                this.SelectedValue = value.ToYNString();
            }
        }

	   public YesNo() {
		   RepeatDirection = System.Web.UI.WebControls.RepeatDirection.Horizontal;
		   RepeatLayout = System.Web.UI.WebControls.RepeatLayout.Flow;
		   Items.Add(new ListItem("Yes","Y"));
		   Items.Add(new ListItem("No","N"));
	   }

	   public string BindProperty { get; set; }

        public void BindFromObject(object obj) {
		   PropertyInfo info=obj.GetType().GetProperty(BindProperty);
		   if(info!=null) {
			   object v = info.GetValue(obj,null);
			   if(v != null) {
				   this.SelectedValue = ((bool)v).ToYN();
			   }
		   }
        }

        public string BindTranslator { get; set; }

	   public void BindToObject(object obj) {
		   PropertyInfo info=obj.GetType().GetProperty(BindProperty);
		   if(info!=null) {
			   info.SetValue(obj,Convert.ChangeType(this.SelectedValue,info.PropertyType),null);
		   }
	   }
    }
}
